/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.put.netbeans.dcs_modeler;

import java.awt.BorderLayout;
import java.io.Serializable;
import java.util.logging.Logger;
import javax.swing.JComponent;
import org.openide.util.NbBundle;
import org.openide.util.lookup.Lookups;
import org.openide.windows.CloneableTopComponent;
import org.openide.windows.TopComponent;
import org.openide.windows.WindowManager;
//import org.openide.util.Utilities;
import org.put.netbeans.dcs_modeler.palette.PaletteSupport;

/**
 * Top component which displays something.
 */
final public class ModelerMainWindowTopComponent extends CloneableTopComponent
{

    private static ModelerMainWindowTopComponent instance;
    /** path to the icon used by the component and its open action */
//    static final String ICON_PATH = "SET/PATH/TO/ICON/HERE";
    private static final String PREFERRED_ID = "ModelerMainWindowTopComponent";
    private JComponent view;
    private GraphSceneImpl scene;
    
    public ModelerMainWindowTopComponent()
    {
        initComponents();
        setName( NbBundle.getMessage( ModelerMainWindowTopComponent.class, "CTL_ModelerMainWindowTopComponent" ) );
        setToolTipText( NbBundle.getMessage( ModelerMainWindowTopComponent.class, "HINT_ModelerMainWindowTopComponent" ) );
//        setIcon(Utilities.loadImage(ICON_PATH, true));
        scene = new GraphSceneImpl();
        view = scene.createView();

        modelerPane.setViewportView( view );
        add( scene.createSatelliteView(), BorderLayout.WEST );
        associateLookup( Lookups.fixed( new Object[]{ PaletteSupport.createPalette() } ) );
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        modelerPane = new javax.swing.JScrollPane();

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(modelerPane, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(modelerPane, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 278, Short.MAX_VALUE)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane modelerPane;
    // End of variables declaration//GEN-END:variables
    /**
     * Gets default instance. Do not use directly: reserved for *.settings files only,
     * i.e. deserialization routines; otherwise you could get a non-deserialized instance.
     * To obtain the singleton instance, use {@link findInstance}.
     */
    public static synchronized ModelerMainWindowTopComponent getDefault()
    {
        if ( instance == null )
        {
            instance = new ModelerMainWindowTopComponent();
        }
        return instance;
    }

    /**
     * Obtain the ModelerMainWindowTopComponent instance. Never call {@link #getDefault} directly!
     */
    public static synchronized ModelerMainWindowTopComponent findInstance()
    {
        TopComponent win = WindowManager.getDefault().findTopComponent( PREFERRED_ID );
        if ( win == null )
        {
            Logger.getLogger( ModelerMainWindowTopComponent.class.getName() ).warning(
                    "Cannot find " + PREFERRED_ID + " component. It will not be located properly in the window system." );
            return getDefault();
        }
        if ( win instanceof ModelerMainWindowTopComponent )
        {
            return ( ModelerMainWindowTopComponent ) win;
        }
        Logger.getLogger( ModelerMainWindowTopComponent.class.getName() ).warning(
                "There seem to be multiple components with the '" + PREFERRED_ID +
                "' ID. That is a potential source of errors and unexpected behavior." );
        return getDefault();
    }

    @Override
    public int getPersistenceType()
    {
        return TopComponent.PERSISTENCE_ALWAYS;
    }

    @Override
    public void componentOpened()
    {
    // TODO add custom code on component opening
    }

    @Override
    public void componentClosed()
    {
    // TODO add custom code on component closing
    }

    /** replaces this in object stream */
    @Override
    public Object writeReplace()
    {
        return new ResolvableHelper();
    }

    @Override
    protected String preferredID()
    {
        return PREFERRED_ID;
    }

    final static class ResolvableHelper implements Serializable
    {

        private static final long serialVersionUID = 1L;

        public Object readResolve()
        {
            return ModelerMainWindowTopComponent.getDefault();
        }
    }
}
